package org.jeecg.modules.demo.crm.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.demo.crm.entity.CPaymentInvoice;
import org.jeecg.modules.demo.crm.mapper.CInvoiceSplitMapper;
import org.jeecg.modules.demo.crm.mapper.CPaymentInvoiceMapper;
import org.jeecg.modules.demo.crm.service.ICPaymentInvoiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 收票登记
 * @Author: jeecg-boot
 * @Date:   2025-06-14
 * @Version: V1.0
 */
@Service
public class CPaymentInvoiceServiceImpl extends ServiceImpl<CPaymentInvoiceMapper, CPaymentInvoice> implements ICPaymentInvoiceService {

    @Autowired
    CPaymentInvoiceMapper paymentInvoiceMapper;

    @Autowired
    CInvoiceSplitMapper cInvoiceSplitMapper;

    @Override
    public List<CPaymentInvoice> loadinvoiceSplit(String projectId, String subProjectId,String requestId) {
        if(requestId == null || "".equals(requestId)){
            requestId ="";
        }
        //查询可以拆票的数据
        List<CPaymentInvoice> list = paymentInvoiceMapper.findiInvoiceSplit();
        List<CPaymentInvoice> filteredList = list.stream()
                .filter(invoice ->(projectId == null || invoice.getProjectId().equals(projectId))
                        && (subProjectId == null || invoice.getSubProjectId().equals(subProjectId)))
                .collect(Collectors.toList());

        //查询拆分记录
        String finalRequestId = requestId;
        filteredList.forEach(invoice -> {

            //此打款记录关联的金额
            BigDecimal splitAmount = cInvoiceSplitMapper.findbyInvoiceAndRequestId(invoice.getId(), finalRequestId);
            //未关联的金额
            BigDecimal splitAmountNot = cInvoiceSplitMapper.findbyInvoiceNot(invoice.getId(), finalRequestId);
            if( invoice.getTotalAmountWithTax() == null){
                invoice.setTotalAmountWithTax(BigDecimal.ZERO);
            }
            BigDecimal highestSplitAmount = invoice.getTotalAmountWithTax().subtract(splitAmountNot);
            invoice.setSplitAmount(splitAmount);
            //最高
            invoice.setHighestSplitAmount(highestSplitAmount);
            //不在本单分配金额
            invoice.setSplitAmountNot(splitAmountNot);
        });

        return filteredList;
    }


    @Override
    public void audit(CPaymentInvoice cPaymentInvoice) {
        CPaymentInvoice cPaymentInvoice1 = paymentInvoiceMapper.selectById(cPaymentInvoice.getId());
        cPaymentInvoice1.setInvoiceStatus(cPaymentInvoice.getInvoiceStatus());
        paymentInvoiceMapper.updateById(cPaymentInvoice1);
    }

    @Override
    public void add(CPaymentInvoice cPaymentInvoice) {
        if(StringUtils.isNotEmpty(cPaymentInvoice.getInvoiceNumber())){
            CPaymentInvoice paymentInvoice =  paymentInvoiceMapper.getInvoiceNumber(cPaymentInvoice.getInvoiceNumber());
            if(paymentInvoice != null){
                throw new RuntimeException("发票号重复");
            }
        }
        paymentInvoiceMapper.insert(cPaymentInvoice);
    }

    @Override
    public void edit(CPaymentInvoice cPaymentInvoice) {
        if(StringUtils.isNotEmpty(cPaymentInvoice.getInvoiceNumber())){
            CPaymentInvoice paymentInvoice = paymentInvoiceMapper.getInvoiceNumber(cPaymentInvoice.getInvoiceNumber());
            if(paymentInvoice != null
                    && !paymentInvoice.getInvoiceNumber().
                    equals(cPaymentInvoice.getInvoiceNumber())){
                throw new RuntimeException("发票号重复");
            }
        }
        paymentInvoiceMapper.updateById(cPaymentInvoice);
    }


}
